package com.lr.controller;

import com.github.pagehelper.PageInfo;
import com.lr.entity.*;
import com.lr.service.IndexService;
import com.lr.util.WordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class IndexController extends HttpServlet {

    @Autowired
    private IndexService indexService;
    @Value("${ArtName}")
    private String ArtName;
    @Value("${VDirPath}")
    private String VDirPath;


    @RequestMapping("/getHeadMenuaop")
    public List<Menu> getHeadMenuaop(){
        return indexService.getHeadMenu();
    }

    @RequestMapping("/getRightMenuaop")
    public List<Menu> getRightMenuaop(){
        return indexService.getRightMenu();
    }
    @RequestMapping("/getBottom1Menuaop")
    public List<Menu> getBottom1Menuaop(){
        return indexService.getBottom1Menu();
    }
    @RequestMapping("/getBottom2Menuaop")
    public List<Menu> getBottom2Menuaop(){
        return indexService.getBottom2Menu();
    }
    @RequestMapping("/getArticleById")
    public Article getArticleById(Integer id){
        return indexService.getArticleById(id);
    }
    @RequestMapping("/getArticlesByMenuType")
    public List<Article> getArticlesByMenuType(Integer menuType){
        return indexService.getArticlesByMenuType(menuType);
    }
    @RequestMapping("/articleDown")
    protected void articleDown(HttpServletRequest req, HttpServletResponse resp,Integer id) throws ServletException, IOException {
        Article article=indexService.getArticleById(id);
        try
        {
            /**
             准备Word模板【xxxx.docx】
             文本占位符: {{name}}
             图片占位符：{{@picture}}
             */
            Map<String,Object> map=new HashMap<String,Object>();
            map.put("articleName",article.getArticleName());
            map.put("createTime",article.getCreateTime());
            map.put("articleTxt",article.getArticleTxt());
            //模版地址
            String dirPath  = VDirPath+ File.separator +ArtName;
            WordUtil.createWordAndDownload(resp, map, dirPath,article.getArticleName()+".docx");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @RequestMapping("/getRecruit")
    public PageInfo getRecruit(@RequestParam(value = "pageSize") Integer pageSize, @RequestParam(value = "pageIndex")Integer pageIndex){
        return indexService.getRecruit(pageSize,pageIndex);
    }
    @RequestMapping("/getResume")
    public PageInfo getResume(@RequestParam(value = "pageSize") Integer pageSize, @RequestParam(value = "pageIndex")Integer pageIndex){
        return indexService.getResume(pageSize,pageIndex);
    }
    @RequestMapping("/getJobFair")
    public PageInfo getJobFair(@RequestParam(value = "pageSize") Integer pageSize, @RequestParam(value = "pageIndex")Integer pageIndex){
        return indexService.getJobFair(pageSize,pageIndex);
    }
    @RequestMapping("/getNews")
    public PageInfo getNews(@RequestParam(value = "pageSize") Integer pageSize, @RequestParam(value = "pageIndex")Integer pageIndex){
        return indexService.getNews(pageSize,pageIndex);
    }
    @RequestMapping("/getPer")
    public PageInfo getPer(@RequestParam(value = "pageSize") Integer pageSize, @RequestParam(value = "pageIndex")Integer pageIndex){
        return indexService.getper(pageSize,pageIndex);
    }
    @RequestMapping("/getRecru")
    public PageInfo getRecru(@RequestParam(value = "pageSize") Integer pageSize, @RequestParam(value = "pageIndex")Integer pageIndex){
        return indexService.getRecru(pageSize,pageIndex);
    }
    @RequestMapping("/getResumeGo")
    public List<Resume> getResumeGo(Resume resume){
        return indexService.getResumeByLink(resume);
    }
    @RequestMapping("/getRecruitGo")
    public List<Recruit> getRecruitGo(Recruit recruit){
        return indexService.getRecruitByLink(recruit);
    }
    @RequestMapping("/getJobFairGO")
    public List<JobFair> getJobFairGO(JobFair jobFair){
        return indexService.getJobFairByLink(jobFair);
    }
    @RequestMapping("/getJobFairById")
    public JobFair getJobFairById(Integer id){
        return indexService.getJobFairById(id);
    }
    @RequestMapping("/getResumeById")
    public Resume getResumeById(Integer id){
        return indexService.getResumeById(id);
    }
    @RequestMapping("/getRecruitById")
    public Recruit getRecruitById(Integer id){
        return indexService.getRecruitById(id);
    }

}
